<?LassoScript
/*
Tagdocs;
	{Tagname=			it_fallback }
	{Description=		fallback handler for itPage }
	{Author=			Eric Landmann }
	{AuthorEmail=		support@iterate.ws }
	{ModifiedBy=		 }
	{ModifiedByEmail=	 }
	{Date=				12/22/11 }
	{Usage=				 }
	{ExpectedResults=	Acts as an include (used in the place of include), first looking in the
						/modules/local/... folders to see if the file exists.
						If it doesn't exist in there, then fallback to the /modules/base/... folders.
						This tag looks in subfolders controllers, helpers and views.
						}
	{Dependencies=		To funcition it needs a file to process, otherwise no dependencies }
	{DevelNotes=		Original idea for this came from the Magento codebase }
	{ChangeNotes=		12/22/11
						First implementation.
						}
/Tagdocs;
*/

If(!(Lasso_TagExists('it_fallback')));

	Define_Tag('fallback', 
		-namespace='it_',
		-description = 'This tag will load code from the Local folder if it exists. It uses a File_ListDirectory to read the contents of the folders in /app/modules/local.',
		-required = 'Filename');

		// TEST FILENAMES
		// Local Controller filepath - /app/modules/local/controllers/LocalControllerTest.inc
		// Local Helper filepath     - /app/modules/local/helpers/LocalHelpersTest.inc
		// Local Views filepath      - /app/modules/local/views/LocalViewsTest.inc
		
		Local('useThis' = string);
		Local('FilenameToCheck' = #Filename);
		Local('ActualFilename' = (#FilenameToCheck->split('/')->last));
		Local('BasePath' = $svBasePath);
		Local('LocalPath' = $svLocalPath);
		Local('Result' = string);
		Local('DebugComment' = string);
		Local('ErrorPath' = false);

		Local('LocalControllersArray' = File_ListDirectory($svLocalControllersPath));
		Local('LocalHelpersArray' = File_ListDirectory($svLocalHelpersPath));
		Local('LocalViewsArray' = File_ListDirectory($svLocalViewsPath));

		// Remove junk from the array
		If(#LocalControllersArray >> '.svn/');
			#LocalControllersArray->Remove(1);
		/If;
		If(#LocalHelpersArray >> '.svn/');
			#LocalHelpersArray->Remove(1);
		/If;
		If(#LocalViewsArray >> '.svn/');
			#LocalViewsArray->Remove(1);
		/If;
		#DebugComment += ('<p class="DebugCT"><strong>it_fallback</strong><br>\n');
		#DebugComment += ('<strong>FilenameToCheck</strong> = '+(#FilenameToCheck)+'<br>\n');
		#DebugComment += ('ActualFilename = '+(#ActualFilename)+'<br>\n');
		#DebugComment += ('LocalControllersArray = '+(#LocalControllersArray)+'<br>\n');
		#DebugComment += ('LocalHelpersArray = '+(#LocalHelpersArray)+'<br>\n');
		#DebugComment += ('LocalViewsArray = '+(#LocalViewsArray)+'<br>\n');

			// Check array with existing filenames to save a file lookup.
			// If the file being examined is in the Local files array, then load the file, otherwise load the base file.
			Select(#FilenameToCheck);

				Case(#FilenameToCheck->EndsWith('/'));
					#DebugComment += ('<strong>ERROR: Invalid Path</strong> = '+(#BasePath)+'<br>');
					#useThis = ('ERROR: Invalid Path: '+(#BasePath));
					#ErrorPath = true;

				Case(#FilenameToCheck->BeginsWith($svLocalControllersPath));
					If(#LocalControllersArray->contains(#ActualFilename));
						#DebugComment += ('File Exists = true<br>\n');
						#DebugComment += ('<strong>INCLUDE THIS Local Controller:</strong> *** '+(#FilenameToCheck)+'***<br>');
						#useThis = #FilenameToCheck;
					Else;
						#DebugComment += ('File Exists = false<br>\n');
						#DebugComment += ('<strong>INCLUDE THIS Base Controller:</strong> *** '+(($svBaseControllersPath)+(#ActualFilename))+'***<br>');
						#useThis = (($svBaseControllersPath)+(#ActualFilename));
					/If;

				Case(#FilenameToCheck->BeginsWith($svLocalHelpersPath));
					If(#LocalHelpersArray->contains(#ActualFilename));
						#DebugComment += ('File Exists = true<br>\n');
						#DebugComment += ('<strong>INCLUDE THIS Local Helper:</strong> *** '+(#FilenameToCheck)+'***<br>');
						#useThis = #FilenameToCheck;
					Else;
						#DebugComment += ('File Exists = false<br>\n');
						#DebugComment += ('<strong>INCLUDE THIS Base Helper:</strong> *** '+(($svBaseHelpersPath)+(#ActualFilename))+'***<br>');
						#useThis = (($svBaseHelpersPath)+(#ActualFilename));
					/If;

				Case(#FilenameToCheck->BeginsWith($svLocalViewsPath));
					If(#LocalViewsArray->contains(#ActualFilename));
						#DebugComment += ('File Exists = true<br>\n');
						#DebugComment += ('<strong>INCLUDE THIS Local View:</strong> *** '+(#FilenameToCheck)+'***<br>');
						#useThis = #FilenameToCheck;
					Else;
						#DebugComment += ('File Exists = false<br>\n');
						#DebugComment += ('<strong>INCLUDE THIS Base View:</strong> *** '+(($svBaseViewsPath)+(#ActualFilename))+'***<br>');
						#useThis = (($svBaseViewsPath)+(#ActualFilename));
					/If;

			/Select;

		// Output debugging comments
		If($svDebug == 'Y');
			Return(#DebugComment);
		// Otherwise, process the include file
		Else;
			If(#ErrorPath == false);
				Return(Process(Include(#useThis)));
			Else;
				Return(#useThis);
			/If;
		/If;

	/Define_Tag;

	Log_Critical('Custom Tag Loaded - it_fallback');

/If;

?>